package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.vioao.wechat.api.component.ComponentApi;
import com.github.vioao.wechat.api.component.wxa.CodeApi;
import com.github.vioao.wechat.bean.MediaFile;
import com.github.vioao.wechat.bean.entity.component.wxa.Category;
import com.github.vioao.wechat.bean.entity.component.wxa.Commit;
import com.github.vioao.wechat.bean.entity.component.wxa.SubmitAudit;
import com.github.vioao.wechat.bean.entity.component.wxa.SubmitAudit.Item;
import com.github.vioao.wechat.bean.response.BaseResponse;
import com.github.vioao.wechat.bean.response.component.GetAuthorizerInfoResponse;
import com.github.vioao.wechat.bean.response.component.wxa.GetAuditstatusResponse;
import com.github.vioao.wechat.bean.response.component.wxa.GetCategoryResponse;
import com.github.vioao.wechat.bean.response.component.wxa.GetPageResponse;
import one.stand.code.RedisCode;
import one.stand.config.ExtJsonConfig;
import one.stand.enums.CompanyVersionEnums;
import one.stand.enums.LoginEnum;
import one.stand.model.CompanyAccountModel;
import one.stand.model.CompanyVersionModel;
import one.stand.model.VersionModel;
import one.stand.service.CodeService;
import one.stand.service.ComponentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: QIK
 * @CreateDate: 2019/5/13 12:02
 */
@Service
public class CodeServiceImpl extends PublicBaseService implements CodeService {
    private ComponentApi componentApi = new ComponentApi();
    @Lazy
    @Autowired
    private ComponentService componentService;
    @Lazy
    @Autowired
    private ExtJsonConfig extJsonConfig;

    @Override
    public BaseResponse commit(String appId, Integer versionId, boolean submitAudit) {
        String token = componentService.getAuthorizerToken(appId);
        BaseResponse baseResponse = new BaseResponse();
        if (StringUtils.isEmpty(token)) {
            baseResponse.setErrmsg("账号未授权");
            baseResponse.setErrcode(61003);
        }
        VersionModel outModel = versionMapper.selectByPrimaryKey(versionId);
        CompanyAccountModel accountModel = companyAccountMapper.select(new CompanyAccountModel() {{
            setAppId(appId);
        }});
        String companyKey;
        String jumpAppid;
        String jumpName;
        String myAppName=accountModel.getAppName();
        if (outModel.getVersionType().equals(LoginEnum.USER.getAccountType())) {
            CompanyAccountModel companyAccountModel = companyAccountMapper.select(new CompanyAccountModel() {{
                setCompanyId(accountModel.getCompanyId());
                setAccountType(LoginEnum.CRM.getAccountType());
            }});
            companyKey = companyAccountModel.getCompanyKey();
            jumpName = companyAccountModel.getAppName();
            jumpAppid = companyAccountModel.getAppId();
        } else {
            CompanyAccountModel companyAccountModel = companyAccountMapper.select(new CompanyAccountModel() {{
                setCompanyId(accountModel.getCompanyId());
                setAccountType(LoginEnum.USER.getAccountType());
            }});
            companyKey = companyAccountModel.getCompanyKey();
            jumpName = companyAccountModel.getAppName();
            jumpAppid = companyAccountModel.getAppId();
        }
        Integer accountId = accountModel.getAccountId();
        GetAuthorizerInfoResponse response = componentApi.getAuthorizerInfo(componentService.getComponentToken(), componentConfig.getAppid(), appId);

        String headImg = "";
        if (response.getAuthorizerInfo() != null) {
            headImg = response.getAuthorizerInfo().getHeadImg();
        }
        log.info("设置头像：{}", headImg);
        if (null != outModel) {
            String json = extJsonConfig.getExtJSON(appId, jumpAppid, jumpName, companyKey, headImg,myAppName).toJSONString();
            log.info("最后的ext_json:{}", json);
            Commit commit = Commit.builder().extJson(json).templateId(outModel.getTemplateId()).userDesc(outModel.getDesc()).userVersion(outModel.getVersionNum()).build();
            baseResponse = CodeApi.commit(token, commit);
        }
        CompanyVersionModel companyVersionModel = new CompanyVersionModel();
        companyVersionModel.setAccountId(accountId);
        companyVersionModel.setVersionId(versionId);
        companyVersionModel.setRemark(baseResponse.toString());
        companyVersionModel.setCreateTime(currentDate());
        if (baseResponse.getErrcode().equals(0)) {
            log.info("代码提交成功：{}", appId);
            companyVersionModel.setState(CompanyVersionEnums.commit_success.getCode());
            companyVersionMapper.insertSelective(companyVersionModel);
            log.info("是否进行代码审核提交：{}", submitAudit);
            if (submitAudit) {
                submitAudit(versionId,appId);
            }
        } else {
            log.info("代码提交失败：{}-{}", appId, baseResponse.toString());
            companyVersionModel.setState(CompanyVersionEnums.commit_err.getCode());
            companyVersionMapper.insertSelective(companyVersionModel);
        }
        return baseResponse;
    }

    @Override
    public String getQrCode(String appid) {
        try {
            String url=redisUtil.get(RedisCode.getQrCodeKey(appid));
            if(StringUtils.isEmpty(url)){
                MediaFile mediaFile = CodeApi.getQrCode(componentService.getAuthorizerToken(appid));
                InputStream inputStream = upload(mediaFile.getBufferedImage(), mediaFile.getSuffix());
                url=ossUtil.uploadObjectOSS(inputStream, mediaFile.getSuffix());
                if(StringUtils.isEmpty(url)){
                    redisUtil.set(RedisCode.getQrCodeKey(appid),url);
                }
            }
            return url;
        } catch (Exception e) {
            log.info("获取体验码失败：{}", appid);
            return null;
        }

    }

    public InputStream upload(BufferedImage bimg, String frefix) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        //保存新图片
        ImageIO.write(bimg, frefix, os);
        InputStream is = new ByteArrayInputStream(os.toByteArray());
        return is;
    }

    @Override
    public BaseResponse submitAudit(Integer versionId,String appId) {
        CompanyVersionModel companyVersionModel = companyVersionMapper.selectAccountById(new CompanyVersionModel() {{
            setVersionId(versionId);
            setAppId(appId);
        }});
        if (null == companyVersionModel) {
            log.info("获取版本数据为空：{}", versionId);
            return null;
        } else {
            String token = componentService.getAuthorizerToken(companyVersionModel.getAppId());
            GetCategoryResponse response = CodeApi.getCategory(token);
            GetPageResponse getPageResponse = CodeApi.getPage(token);
            if (null == getPageResponse.getPageList() || getPageResponse.getPageList().size() <= 0) {
                //添加默认值
                List<String> list = new ArrayList<>();
                list.add("pages/index/main");
                getPageResponse.setPageList(list);
            }
            List<Category> list = response.getCategoryList();
            List<Item> itemList = new ArrayList<>();
            list.forEach(l -> {
                Item item = Item.builder()
                        .address(getPageResponse.getPageList().get(0))
                        .firstClass(l.getFirstClass())
                        .firstId(l.getFirstId())
                        .secondClass(l.getSecondClass())
                        .secondId(l.getSecondId())
                        .title("one").build();
                itemList.add(item);
            });
            SubmitAudit submitAudit = new SubmitAudit(itemList);
            BaseResponse baseResponse = CodeApi.submitAudit(token, submitAudit);
            if (baseResponse.getErrcode().equals(0)) {
                companyVersionModel.setState(CompanyVersionEnums.in_audit.getCode());
            } else {
                companyVersionModel.setState(CompanyVersionEnums.commit_audit.getCode());
            }
            companyVersionModel.setRemark(JSON.toJSONString(baseResponse));
            companyVersionMapper.updateByPrimaryKeySelective(companyVersionModel);
            return baseResponse;
        }
    }

    @Override
    public GetCategoryResponse getCategory(String appid) {
        return CodeApi.getCategory(componentService.getAuthorizerToken(appid));
    }

    @Override
    public GetPageResponse getPage(String appid) {
        return CodeApi.getPage(componentService.getAuthorizerToken(appid));
    }

    @Override
    public GetAuditstatusResponse getLatestAuditStatus(Integer versionId, String appId,boolean release) {
        CompanyVersionModel companyVersionModel = companyVersionMapper.selectAccountById(new CompanyVersionModel() {{
            setVersionId(versionId);
            setAppId(appId);
        }});
        String token = componentService.getAuthorizerToken(companyVersionModel.getAppId());
        GetAuditstatusResponse response = CodeApi.getLatestAuditStatus(token);
        log.info("获取最新审核结果：{}", response.toString());
        if (response.getErrcode().equals(0)) {
            if (response.getStatus().equals(0)) {
                if(release){
                    log.info("审核通过，开始发布");
                    release(companyVersionModel);
                }
                companyVersionModel.setState(CompanyVersionEnums.audit_success.getCode());
                companyVersionModel.setRemark(response.toString());
                companyVersionMapper.updateByPrimaryKeySelective(companyVersionModel);
            } else {
                if(response.getStatus()==1){
                    companyVersionModel.setState(CompanyVersionEnums.audit_err.getCode());
                }else{
                    companyVersionModel.setState(CompanyVersionEnums.in_audit.getCode());
                }
                companyVersionModel.setRemark(response.toString());
                companyVersionMapper.updateByPrimaryKeySelective(companyVersionModel);
            }
        }
        return response;
    }

    @Override
    public BaseResponse release(Integer versionId, String appId) {
        CompanyVersionModel companyVersionModel = companyVersionMapper.selectAccountById(new CompanyVersionModel() {{
            setVersionId(versionId);
            setAppId(appId);
        }});
        if(companyVersionModel.getState().equals(CompanyVersionEnums.release_success.getCode())){
            log.info("当前小程序没有待发布工作");
        }
        BaseResponse response = CodeApi.release(componentService.getAuthorizerToken(companyVersionModel.getAppId()));
        log.info("小程序开始发布..........{}", response.toString());
        if (response.getErrcode().equals(0)) {
            companyVersionModel.setState(CompanyVersionEnums.release_success.getCode());
        } else {
            companyVersionModel.setState(CompanyVersionEnums.release_err.getCode());
        }
        companyVersionModel.setRemark(response.toString());
        companyVersionMapper.updateByPrimaryKeySelective(companyVersionModel);
        return response;
    }

    @Override
    public BaseResponse release(CompanyVersionModel companyVersionModel) {
        BaseResponse response = CodeApi.release(componentService.getAuthorizerToken(companyVersionModel.getAppId()));
        log.info("小程序开始发布..........{}", response.toString());
        if (response.getErrcode().equals(0)) {
            companyVersionModel.setState(CompanyVersionEnums.release_success.getCode());
        } else {
            companyVersionModel.setState(CompanyVersionEnums.release_err.getCode());
        }
        companyVersionModel.setRemark(response.toString());
        companyVersionMapper.updateByPrimaryKeySelective(companyVersionModel);
        return response;
    }

    @Override
    public BaseResponse undoCodeAudit(String appId) {
        return CodeApi.undoCodeAudit(componentService.getAuthorizerToken(appId));
    }
}


